package jaeger

import (
	"io"

	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	jconfig "github.com/uber/jaeger-client-go/config"
)

const (
	defaultHeaderName = "x-trace-id"
)

// Config ...
type Config struct {
	ServiceName string
	Disabled    bool
	RPCMetrics  bool

	Sampler             *jconfig.SamplerConfig
	Reporter            *jconfig.ReporterConfig
	Headers             *jaeger.HeadersConfig
	BaggageRestrictions *jconfig.BaggageRestrictionsConfig
	Throttler           *jconfig.ThrottlerConfig
	Tags                []opentracing.Tag

	options []jconfig.Option
}

// WithTag ...
func (cfg *Config) WithTag(tags ...opentracing.Tag) *Config {
	if cfg.Tags == nil {
		cfg.Tags = make([]opentracing.Tag, 0)
	}
	cfg.Tags = append(cfg.Tags, tags...)
	return cfg
}

// WithOption ...
func (cfg *Config) WithOption(options ...jconfig.Option) *Config {
	if cfg.options == nil {
		cfg.options = make([]jconfig.Option, 0, len(options))
	}
	cfg.options = append(cfg.options, options...)
	return cfg
}

// Build ...
func (cfg *Config) Build() (opentracing.Tracer, io.Closer) {
	if cfg.Headers == nil {
		cfg.Headers = &jaeger.HeadersConfig{
			TraceBaggageHeaderPrefix: "ctx-",
			TraceContextHeaderName:   defaultHeaderName,
		}
	}

	return newTracer(cfg)
}
